package com.axend.sofa.ui.activity

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.axend.lib_base.ext.click
import com.axend.lib_base.view.dialog.CustomClearDialog
import com.axend.sofa.base.BaseBleVVDActivity
import com.axend.lib_service.ARouterPath
import com.axend.sofa.adapter.BleLogAdapter
import com.axend.sofa.databinding.ActivityLogBinding
import com.axend.sofa.viewmodel.LogViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 * @description log
 * @author admins
 * @time 2025/3/10 9:37
 */
@AndroidEntryPoint
@Route(path = ARouterPath.ACTIVITY_LOG)
class LogActivity : BaseBleVVDActivity<LogViewModel, ActivityLogBinding>() {

    @Inject
    lateinit var bleLogAdapter: BleLogAdapter

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        // set adapter
        mBinding.rvLog.apply {
            layoutManager = LinearLayoutManager(this@LogActivity)
            adapter = bleLogAdapter
            setHasFixedSize(true)
        }

        mBinding.swRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                mViewModel.setPageIndex(1)
                refreshLayout.finishRefresh(800)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mViewModel.setPageIndex(mViewModel.pageIndex.value!! + 1)
                refreshLayout.finishLoadMore(800)
            }
        })

        mBinding.titleBar.rightLayout.click {
            CustomClearDialog(
                this, "", getString(com.axend.lib_base.R.string.app_tips_content_clear_log)
            ) { p0, p1 ->
                mViewModel.deleteAllLog()
                p0.dismiss()
            }.show()
        }

        initObserver()
    }

    private fun initObserver() {
        mViewModel.logList.observe(this) {
            bleLogAdapter.setList(it)
        }

        mViewModel.pageIndex.observe(this) {
            mViewModel.getLogList((it - 1) * mViewModel.pageSize.value!!, pageSize = mViewModel.pageSize.value!!)
        }
    }
}